package com.amap.template;

import com.amap.builder.BizResponse;
import com.amap.builder.ResponseBuilder;
import com.amap.constants.Constants;
import com.amap.exception.AmapException;
import com.amap.handler.AmapBusinessHandler;
import com.amap.sign.SignVerifier;
import java.util.Map;
/**
 * @author liuyi
 * @since 2025/10/31 11:18
 */
public class AmapServiceTemplate {
    private final SignVerifier signVerifier;
    private final boolean signEnabled;

    public BizResponse processRequest(AmapBusinessHandler businessHandler, Map<String, String> requestMap) {
        try {
            if (this.signEnabled) {
                this.signVerifier.verify(requestMap);
            }
            String bizContent = (String)requestMap.get(Constants.PARAM_BIZ_CONTENT);
            if (bizContent != null && !bizContent.isEmpty()) {
                Object result = businessHandler.handle(bizContent);
                return ResponseBuilder.success(result);
            } else {
                return ResponseBuilder.fail("40001", "biz_content参数缺失");
            }
        } catch (AmapException e) {
            return ResponseBuilder.fail("40002", "签名验证失败：" + e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.fail("40004", "业务处理异常：" + e.getMessage());
        }
    }

    public AmapServiceTemplate(SignVerifier signVerifier, boolean signEnabled) {
        this.signVerifier = signVerifier;
        this.signEnabled = signEnabled;
    }
}
